package com.tian.controller;

import com.tian.pojo.Doctor;
import com.tian.pojo.User;
import com.tian.service.DoctorService;
import com.tian.service.UserService;
import com.tian.vo.PageResult;
import com.tian.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/doctor")
public class DoctorController {


    @Autowired
    private DoctorService doctorService;

    @GetMapping("/getDoctorList")
    public SysResult getDoctorList(PageResult pageResult){
        pageResult = doctorService.getDoctorList(pageResult);
        return SysResult.success(pageResult);
    }

    @DeleteMapping("/deleteDoctorById")
    public SysResult deleteUserById (Integer id){
        doctorService.deleteUserById(id);
        return SysResult.success();
    }

    @PostMapping("/saveDoctor")
    @Transactional  // 1.运行期异常 2.编译异常(检查异常)
    // 事务默认的条件下只对运行时异常有效
    public SysResult addUser(@RequestBody Doctor doctor){
        if (doctor.getBygood()==""||doctor.getAge()<=0||doctor.getDepartment()==""||doctor.getName()==""||doctor.getSex()=="")
            return SysResult.fail();
        doctorService.saveDoctor(doctor);
        return SysResult.success();
    }

}
